package com.mlog.njyjy.ndop.msg.relay.exception;

import com.mlog.njyjy.ndop.msg.relay.domain.StandardField;

import cn.hutool.core.util.StrUtil;

/**
 * @author zyz
 * @version v1.0, 2020/8/4
 */
public class ValidateException extends RuntimeException {


    /**
	 * 
	 */
	private static final long serialVersionUID = -152789444514901611L;

	private String fieldName;

    private Object fieldValue;

    private String dictName;

    private String rule;
    
    private int errorCode;
    
    private String body;
    
    public int getErrorCode() {
    	return errorCode;
    }
    

    public String getBody() {
		return body;
	}


	public void setBody(String body) {
		this.body = body;
	}
	
	public ValidateException(StandardField sf, Object value, String body) {
        super(String.format(sf.getErrorMsg(), value));
        this.fieldName = sf.getName();
        this.fieldValue = value;
        this.body = body;
    }


	public ValidateException(int errorCode, String message, String body) {
        super(message);
        this.body = body;
        this.errorCode = errorCode;
    }

    public ValidateException(int errorCode, String message, String fieldName, Object fieldValue) {
        super(message);
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
        this.errorCode = errorCode;
    }

    public ValidateException(int errorCode, String message, String fieldName, String dictName) {
        super(message);
        this.fieldName = fieldName;
        this.dictName = dictName;
        this.errorCode = errorCode;
    }

    public ValidateException(int errorCode, String message, String fieldName, Object fieldValue, String dictName) {
        super(message);
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
        this.dictName = dictName;
        this.errorCode = errorCode;
    }

    public ValidateException(int errorCode, String message, String fieldName, Object fieldValue, String dictName, String rule) {
        super(message);
        this.errorCode = errorCode;
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
        this.dictName = dictName;
        this.rule = rule;
    }

    public ValidateException(Throwable cause) {
        super(cause);
    }

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param message the detail message. The detail message is saved for
     *                later retrieval by the {@link #getMessage()} method.
     */
    public ValidateException(String message, String body) {
        super(message);
        this.body = body;
    }

    @Override
    public String getMessage() {
        StringBuilder builder = new StringBuilder();
        builder.append(super.getMessage());
        if (StrUtil.isNotBlank(fieldName)) {
            builder.append("\n fieldName is : ").append(fieldName);
        }
        if (fieldValue != null && StrUtil.isNotBlank(String.valueOf(fieldValue))) {
            builder.append("\n fieldValue is : ").append(fieldValue);
        }
        if (StrUtil.isNotBlank(dictName)) {
            builder.append("\n dictName is : ").append(dictName);
        }
        if (StrUtil.isNotBlank(rule)) {
            builder.append("\n rule is : ").append(rule);
        }
        return builder.toString();
    }
}
